<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!--<el-form-item label="服务说明" prop="description">-->
        <!--<el-input-->
          <!--v-model="queryParams.description"-->
          <!--placeholder="请输入服务说明"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="标题" prop="detailName">
        <el-input
          v-model="queryParams.detailName"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入费用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订金" prop="deposit">
        <el-input
          v-model="queryParams.deposit"
          placeholder="请输入订金"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用单位">
        <el-select v-model="queryParams.unit" placeholder="选择状态" clearable size="small" prop="unit">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label=" 计费方式">
        <el-select v-model="queryParams.charge" placeholder="选择状态" clearable size="small" prop="charge">
          <el-option
            v-for="dict in Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:detail:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">

      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="detailList"  @selection-change="handleSelectionChange" row-key="typeDetailId"   default-expand-all  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="detailName" />
      <el-table-column label="费用单位" align="center" prop="unit":formatter="statusFormat" />
      <el-table-column label="计费方式" align="center" prop="charge":formatter="Format" />
      <el-table-column label="费用" align="center" prop="money" />
      <el-table-column label="订金" align="center" prop="deposit" />
      <!--<el-table-column label="服务说明" align="center" prop="description" />-->
      <el-table-column label="注意事项" align="center" prop="attention" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分类详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="detailName">
          <el-input v-model="form.detailName" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="上级分类" prop="parentTypeId">
          <treeselect   v-model="form.typeId" :options="typeOptions" placeholder="选择上级分类"  :normalizer="normalizer" @select="treeselectfun"   />
        </el-form-item>
        <el-form-item label=" 费用单位">
          <el-select v-model="form.unit" placeholder="选择费用单位" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="  计费方式">
          <el-select v-model="form.charge" placeholder="选择计费方式" clearable size="small">
            <el-option
              v-for="dict in Options"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="费用" prop="money">
          <el-input v-model="form.money"  name="money"/>
        </el-form-item>
        <el-form-item label="订金" prop="deposit">
          <el-input v-model="form.deposit"/>
        </el-form-item>
        <el-form-item label="注意事项" prop="attention" >
          <el-input v-model="form.attention" placeholder="请输入注意事项" />
        </el-form-item>
        <el-form-item label="服务说明"  prop="description">
            <Editor v-model="form.description"/>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top: 30px;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {list, listType, getType, delType, addType, updateType, exportType , treeselect  } from "@/api/system/type";
import { listDetail, getDetail, delDetail, addDetail, updateDetail, exportDetail } from "@/api/system/detail";
import Editor from '@/components/Editor';
import Treeselect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: "Detail",
  components: {
    Treeselect,
    Editor
  },
  data() {



    return {
      defaultProps: {
        children: 'children',
        label: 'typeName'
      },
      typeOptions: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      statusOptions:[],
      Options:[],
      // 分类详情表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        parentTypeId: undefined,
        pageNum: 1,
        pageSize: 10,
        charge:undefined,
        description: undefined,
        attention: undefined,
        money: undefined,
        deposit: undefined,
        detailName: undefined,
        unit:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {


      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_cost_unit").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_mode").then(response => {
      this.Options = response.data;
    });
  },
  methods: {
    treeselectfun(e){
      console.log(e)
      this.form.sysModel = e.sysModel
    },
    normalizer(node){
      //去掉children=[]的children属性
      if(node.children && !node.children.length){
        delete node.children;
      }
      return {
        id: node.typeId,
        label:node.typeName,
        children:node.children
      }
    },
    /** 查询分类详情列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      list().then(response => {
        this.typeOptions = response.data;
      })
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.unit );
    },
    Format(row, column) {
      return this.selectDictLabel(this.Options, row.charge );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        typeId: undefined,
        parentTypeId: undefined,
        charge:undefined,
        unit:undefined,
        typeDetailId: undefined,
        description: undefined,
        attention: undefined,
        money: undefined,
        deposit: undefined,
        detailName: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.typeDetailId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分类详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const typeDetailId = row.typeDetailId || this.ids
      getDetail(typeDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分类详情";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.typeDetailId != undefined) {
            updateDetail(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addDetail(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const typeDetailIds = row.typeDetailId || this.ids;
      this.$confirm('是否确认删除分类详情编号为"' + typeDetailIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDetail(typeDetailIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有分类详情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDetail(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
